import {Navigate, Outlet, useRoutes} from "react-router";
import RouterMap from "@/router/routerMap.ts";

const AuthGuard = ({ token }: { token: string | null }) => {
    return token ? <Outlet /> : <Navigate to="/login" replace />;
};

function Router () {

    const token = sessionStorage.getItem('token') || ''
    // 公共路由
    const routes = [
        {
            path: "/",
            element: <AuthGuard token={token} />,
            children: [
                {
                    path: '/select',
                    element: <RouterMap.select />,
                    name: 'select'
                },
                {
                    path: "/",
                    element: <RouterMap.layout />,
                    children: [
                        {
                            path: "/",
                            element: <Navigate to="/home" replace />
                        },
                        {
                            path: "home",
                            element: <RouterMap.home />,
                            name: 'home'
                        },
                    ]
                },
                {
                    path: "/system",
                    element: <RouterMap.layout />,
                    children: [
                        {
                            path: "user",
                            element: <RouterMap.user />,
                            name: 'user'
                        },
                        {
                            path: "menu",
                            element: <RouterMap.menu />,
                            name: 'menu'
                        },
                        {
                            path: "role",
                            element: <RouterMap.role />,
                            name: 'role'
                        },
                    ]
                },
            ]
        },
        {
            path: "/login",
            name: 'login',
            element: <RouterMap.login/>,
        },

    ]
    return useRoutes(routes)
}

export default Router